package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCommentImg;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.LabelQueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.ILabelService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.IStrategycatalogService;
import cn.wolfcode.trip.base.util.JasonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/strategys")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService ;
    @Autowired
    private IStrategycatalogService strategycatalogService ;
    @Autowired
    private ILabelService labelService;
    @Autowired
    private IStrategyCommentService strategyCommentService;


    /**
     * 查询指定大攻略中所属的攻略分类
     * 参数：无
     * 资源：/strategys/{id}/catalogs
     * 动作：GET
     */
    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogs(@PathVariable Long strategyId){
        return strategycatalogService.queryCatalogBystrategyId(strategyId);
    }

    /**
     * 获取当前为推荐状态的所有大攻略
     * 参数：state=2
     * 资源：/strategys
     * 动作:GET
     */
    @GetMapping
    public Object getCommends(StrategyQueryObject qo){
        return strategyService.query(qo);
    }

    @GetMapping("/comment/modelLabels")
    public Object getModelLabel(){
        return labelService.getModelLabel();
    }

    @PostMapping("/{strategyId}/comment")
    public Object saveComment(Long[] labels,String[] images,String myTags,Integer grade,String content,@PathVariable Long strategyId){
        JasonResult jasonResult = new JasonResult();
        StrategyComment strategyComment = new StrategyComment();
        strategyComment.setGrade(grade);
        strategyComment.setContent(content);
        Strategy strategy = new Strategy();
        strategy.setId(strategyId);
        strategyComment.setStrategy(strategy);
        try {
            strategyCommentService.save(strategyComment,labels,images,myTags);
        } catch (Exception e) {
            e.printStackTrace();
            jasonResult.mark(e.getMessage());
        }

        return jasonResult;
    }

    /**
     * 获取大攻略点评的十个标签
     * @param id 大攻略id
     * @return
     */
    @GetMapping("/{id}/comment/labels")
    public Object getStrategyLabels(@PathVariable Long id){
        //创建高级查询
        LabelQueryObject qo = new LabelQueryObject();
        //按照标签创建顺序倒序排列
        qo.setOrderBy("l.id desc");
        //设置参数
        qo.setStrategyId(id);
        //一共获取十条
        qo.setPageSize(10);
        return labelService.getLabelsByStrategyId(qo);
    }

    @GetMapping("/{id}/comment")
    public Object getStrategyComment(){
        return null;
    }


    /**
     * 获取大攻略所有点评
     * @param qo
     * @return
     */
    @GetMapping("/{strategyId}/comments")
    public Object strategyComments(StrategyCommentQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo =  strategyCommentService.selectForList(qo);
        return pageInfo;
    }
    /**
     * 获取所有点评
     * @param qo
     * @return
     */
    @GetMapping("/comments")
    public Object allStrategyComments(StrategyCommentQueryObject qo){
        List<StrategyComment> list = strategyCommentService.selectAll();
        return list;
    }

    @GetMapping("/comment/{commentId}")
    public Object getSomeComment(@PathVariable Long commentId){
        return strategyCommentService.getSomeComment(commentId);
    }


}
